/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dpy.appmvdpy.Models.Importers;

import com.dpy.appmvdpy.Facades.ClientesFacade;
import com.dpy.appmvdpy.Facades.VendedorFacade;
import com.dpy.appmvdpy.Models.Clientes;
import com.dpy.appmvdpy.Models.Produtos;
import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.MaskFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 *
 * @author ac_psoares
 */
@Component
@Scope(value="request")
public class ClientImporter extends Importer<Clientes> {
  
    @Autowired
    private VendedorFacade vFacade;    
    
    @Autowired
    private ClientesFacade cFacade;
    
    public ClientImporter()
    {

    }
    
    public ClientImporter(File file)
    {
       super(file);
    }
    
    @Override
    public List<Clientes> doImport() 
    {
         List<Clientes> retorno = new ArrayList<Clientes>();
        try {
            
           int i = 0;
           String record = null;
           MaskFormatter formatCnpj = null;
            try {
                 formatCnpj = new MaskFormatter("##.###.###/####-##");
            } catch (ParseException ex) {
                Logger.getLogger(ClientImporter.class.getName()).log(Level.SEVERE, null, ex);
            }
           while((record = bReader.readLine()) != null)
           {
           
            System.out.println("registro :" + record);
            String[] parametros = record.split("##");
            
            if (parametros.length > 1)
            {
           
           
            Clientes p = cFacade.getByCodigoDpy(parametros[0].trim());
           
            p.setCodigo(parametros[0].trim());
            p.setNome(parametros[3].trim()); 
            p.setNomeFantasia(parametros[4].trim());
            p.setEndereco(parametros[5].trim());
            p.setBairro(parametros[7].trim());
            p.setCep(parametros[8].trim());
            p.setCidade(parametros[9].trim());
            p.setEstado(parametros[10].trim());
            p.setInscricao(parametros[11].trim());
            p.setCnpj(parametros[13].substring(0,2) + "." + parametros[13].substring(2,5) + "." + parametros[13].substring(5,8) + "/" + parametros[13].substring(8,12) + "-" + parametros[13].substring(12));          
            System.out.println(p.getCnpj());
            p.setTelefone(parametros[14].trim());
            p.setFax(parametros[15].trim());
            p.setContato(parametros[16].trim());
            p.setEmail(parametros[18].trim());
            p.setVendedor(vFacade.getByCodigoDPY(parametros[2]));  
            SimpleDateFormat formate = new SimpleDateFormat("ddMMyy");
                try {
                    p.setDataCadastro(formate.parse(parametros[17]));
                } catch (ParseException ex) {
                    Logger.getLogger(ClientImporter.class.getName()).log(Level.SEVERE, null, ex);
                }
            p.setIntegrado(true);
            retorno.add(p);
            }
           }
        } catch (IOException ex) {
            Logger.getLogger(ClientImporter.class.getName()).log(Level.SEVERE, null, ex);
        }
        return retorno;        
    }
}
